//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_BBOX_PROTOCOL_CPEERCONNECTION_H_
#define _BUOLA_BBOX_PROTOCOL_CPEERCONNECTION_H_

#include <buola/bbox/protocol/csyncconnection.h>

#include <buola/traits/signature.h>

namespace buola { namespace bbox { namespace protocol {

class CPeerConnection : public CSyncConnection, public UShared
{
private:
    //this creates a client connection, which will be registered in the scheduler
    CPeerConnection(io::PSocket pSocket);

public:
    virtual ~CPeerConnection();

    template<typename tReturn,typename... tArgs>
    OEnableIf<std::is_same<tReturn,void>,void> CallMethod(uint64_t pService,const std::string &pMethod,const tArgs&... pArgs)
    {
        CStackPacket lPacket(io::nHeader,EOpcode::CALLMETHOD);
        lPacket << pService << pMethod;
        lPacket.Put(pArgs...);

        SendPacket(lPacket);
    }

    template<typename tReturn,typename... tArgs>
    OEnableIf<QNot<std::is_same<tReturn,void>>,tReturn> CallMethod(uint64_t pService,const std::string &pMethod,const tArgs&... pArgs)
    {
        CStackPacket lPacket(io::nHeader,EOpcode::CALLMETHOD);
        lPacket << pService << pMethod;
        lPacket.Put(pArgs...);

        std::unique_ptr<CPacket> lReply(SendPacketAndWaitReply(lPacket));

        if(lReply->Header().IsError())
            throw XConnection("can't call terminal");
    
        tReturn lReturn;
        *lReply >> lReturn;
        return lReturn;
    }
    
    std::pair<uint64_t,uint64_t> ConnectTerminal(uint64_t pService,const std::string &pTerminal);

    void RegisterLink(detail::CLink *pLink);
    void UnregisterLink(detail::CLink *pLink);
    
private:
    void DoCallMethod(CPacket &pPacket);
    void DoConnectTerminal(CPacket &pPacket);
    void DoLinkData(CPacket &pPacket);
    
    void OnPacket(CPacket &pPacket) override;
    void OnEOF() override;

private:
    std::map<uint64_t,detail::CLink*> mLinks;
    
    friend CSession;
};

typedef intrusive_ptr<CPeerConnection> PPeerConnection;

/*namespace protocol*/ } /*namespace bbox*/ } /*namespace buola*/ }

#endif
